//
//  KeleData.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/24.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "KeleData.h"
#import "HttpTools.h"

NSString * const URL_ROOT = @"Http://www.yyssd.com/";

NSString * const SENDCODE = @"userInfo/sendCode";

NSString * const FASTLOGIN = @"userInfo/fastLogin";

NSString * const ACCOUNTLOGIN = @"userInfo/login";

NSString * const REGISTER = @"userInfo/register";

NSString * const FORGETPWD = @"userInfo/change";

NSString * const HOMERECOMMEND = @"recommend/allRecommendByPostId";

@implementation KeleData

static KeleData *instance = nil;

+ (KeleData *)sharedInstance {
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        instance = [[self alloc] init];
    });
    return instance;
}



// 发送验证码
- (void)sendPhoneCodeWithPhone:(NSString *)phone
                       success:(KlResponseSuccess)success
                       failure:(KlResponseFail)failure{
    NSString *url = [NSString stringWithFormat:@"%@%@?phone=%@",URL_ROOT,SENDCODE,phone];
   
    [HttpTools get:url params:nil success:success failure:failure];
}

// 手机号快速登录
- (void)phoneFastLoginWithPhone:(NSString *)phone
                      sessionId:(NSString *)sessionId
                           code:(NSString *)code
                        success:(KlResponseSuccess)success
                        failure:(KlResponseFail)failure{
    
    NSString *url = [NSString stringWithFormat:@"%@%@",URL_ROOT,FASTLOGIN];
    
    NSMutableDictionary * parameters = [@{
                                  @"phone":phone,
                                  @"sessionId":sessionId,
                                  @"code":code,
                                  @"registPlatform":@(1),
                                  @"phoneName":[CheckMobile iPhoneType]
                                  }mutableCopy];
    [KeleData addPublicPrametersWithDic:parameters];
    [HttpTools post:url params:parameters success:success failure:failure];
    
}

// 手机号账户密码登录
- (void)phoneAccountLoginWithPhone:(NSString *)telephone
                          password:(NSString *)password
                           success:(KlResponseSuccess)success
                           failure:(KlResponseFail)failure{
    
    NSString *url = [NSString stringWithFormat:@"%@%@",URL_ROOT,ACCOUNTLOGIN];
    
    NSMutableDictionary * parameters = [@{
                                          @"telephone":telephone,
                                          @"password":password,
                                         
                                          }mutableCopy];
    [KeleData addPublicPrametersWithDic:parameters];
    [HttpTools post:url params:parameters success:success failure:failure];
}


// 注册
- (void)registerWithPhone:(NSString *)telephone
                sessionId:(NSString *)sessionId
                     code:(NSString *)code
                 password:(NSString *)password
                  success:(KlResponseSuccess)success
                  failure:(KlResponseFail)failure{
    
    NSString *url = [NSString stringWithFormat:@"%@%@",URL_ROOT,REGISTER];
    
    NSMutableDictionary * parameters = [@{
                                          @"telephone":telephone,
                                          @"sessionId":sessionId,
                                          @"password":password,
                                          @"code":code,
                                          @"registPlatform":@(1),
                                          @"phoneName":[CheckMobile iPhoneType]
                                          }mutableCopy];
    [KeleData addPublicPrametersWithDic:parameters];
    [HttpTools post:url params:parameters success:success failure:failure];
    
}

// 忘记密码
- (void)forgetPasswordWithPhone:(NSString *)phone
                      sessionId:(NSString *)sessionId
                           code:(NSString *)code
                       password:(NSString *)password
                        success:(KlResponseSuccess)success
                        failure:(KlResponseFail)failure{
    
    NSString *url = [NSString stringWithFormat:@"%@%@",URL_ROOT,FORGETPWD];
    
    NSMutableDictionary * parameters = [@{
                                          @"phone":phone,
                                          @"sessionId":sessionId,
                                          @"password":password,
                                          @"code":code
                                          }mutableCopy];
    [KeleData addPublicPrametersWithDic:parameters];
    [HttpTools post:url params:parameters success:success failure:failure];
    
}

// 首页推荐
- (void)recommendWithPage:(NSString *)currentPage
               pageNumber:(NSString *)pageNumber
                  success:(KlResponseSuccess)success
                  failure:(KlResponseFail)failure{
    NSString *userId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    NSString *userIdStr = userId.length == 0?@"":UserID;
    NSString *url = [NSString stringWithFormat:@"%@%@?userId=%@&currentPage=%@&pageNumber=%@",URL_ROOT,HOMERECOMMEND,userIdStr,currentPage,pageNumber];
    [HttpTools get:url params:nil success:success failure:failure];
    
    
    
}


#pragma mark ---------
+ (NSMutableDictionary *)addPublicPrametersWithDic:(NSMutableDictionary *)parameters{
    [parameters setValue:@"ios" forKey:@"channel"];
    [parameters setValue:APPVersion forKey:@"versionCode"];
    [parameters setValue:UUID forKey:@"deviceId"];
    return parameters;
}

@end
